FROM node:22-slim AS builder

# 设置环境变量
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV NODE_OPTIONS=--max-old-space-size=8192
ENV TZ=Asia/Shanghai

# 安装pnpm
RUN npm i -g corepack

# 设置工作目录
WORKDIR /app

# 复制项目文件到工作目录
COPY . /app

# 安装依赖并构建项目
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

RUN pnpm run build

RUN echo "Builder Success 🎉"

# 生产阶段使用nginx镜像
FROM nginx:stable-alpine AS production

# 配置nginx支持JavaScript模块
RUN echo "types { application/javascript js mjs; }" > /etc/nginx/conf.d/mjs.conf \
    && rm -rf /etc/nginx/conf.d/default.conf

# 从构建阶段复制构建产物到nginx目录
COPY --from=builder /app/apps/web-antd/dist /usr/share/nginx/html

# 复制nginx配置
COPY --from=builder /app/scripts/deploy/nginx.conf /etc/nginx/nginx.conf

# 暴露端口
EXPOSE 5173

# 启动nginx
CMD ["nginx", "-g", "daemon off;"]
